package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.setting.adapter.ImgAdapter;
import com.hzcx.app.simplechat.ui.setting.adapter.ReportAdapter;
import com.hzcx.app.simplechat.ui.setting.bean.ReportBean;
import com.hzcx.app.simplechat.ui.setting.contract.ReportContract;
import com.hzcx.app.simplechat.ui.setting.presenter.ReportPresenter;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.photo.PhotoUtil;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 举报界面
 */
public class ReportActivity extends BaseActivity<ReportPresenter> implements ReportContract.View, BaseQuickAdapter.OnItemClickListener,
        OnResultCallbackListener<LocalMedia>, BaseQuickAdapter.OnItemChildClickListener {

    public static final String INTENT_USER_ID = "INTENT_USER_ID";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_report)
    RecyclerView rvReport;
    @BindView(R.id.rv_img)
    RecyclerView rvImg;
    @BindView(R.id.et_content)
    EditText etContent;

    private List<ReportBean> reportData;
    private ReportAdapter reportAdapter;

    private List<String> imgList;
    private ImgAdapter imgAdapter;
    private List<LocalMedia> seleteImgList;

    @Override
    public int getLayout() {
        return R.layout.activity_report;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ReportPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("投诉");
        tvTitleRight.setText("提交");
        tvTitleRight.setTextColor(getResources().getColor(R.color.color_2D7CFD));

        reportData = new ArrayList<>();
        reportAdapter = new ReportAdapter(reportData);
        rvReport.setLayoutManager(new GridLayoutManager(this, 2));
        rvReport.setAdapter(reportAdapter);

        seleteImgList = new ArrayList<>();
        imgList = new ArrayList<>();
        imgList.add("");
        imgAdapter = new ImgAdapter(R.layout.rv_item_img, imgList);
        rvImg.setLayoutManager(new GridLayoutManager(this, 3));
        rvImg.setAdapter(imgAdapter);

        imgAdapter.setOnItemClickListener(this);
        imgAdapter.setOnItemChildClickListener(this);

        reportAdapter.setOnItemClickListener((adapter, view, position) -> reportAdapter.setPos(position));
    }

    @Override
    public void initData() {
        mPresenter.getReportList(this);
    }

    @OnClick({R.id.tv_title_right})
    public void viewOnClick(View view){
        switch (view.getId()){
            case R.id.tv_title_right:
                showLoading();
                mPresenter.upLoadImg(this,imgList);
                break;
        }
    }

    /**
     * 举报内容获取成功
     *
     * @param data
     */
    @Override
    public void reportResult(List<ReportBean> data) {
        try {
            reportData.clear();
            reportData.addAll(data);
            reportAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 投诉成功
     */
    @Override
    public void putSuccess() {
        showError("投诉成功");
        finish();
    }

    /**
     * 图片上传成功
     *
     * @param url
     */
    @Override
    public void upLoadSuccess(String url) {
        mPresenter.putReport(this, getIntent().getIntExtra(INTENT_USER_ID, 0),
                reportData.get(reportAdapter.getPos()).getComplaint_id(), etContent.getText().toString(),url);
    }

    /**
     * 图片列表点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (EmptyUtils.isEmpty(imgList.get(position))) {
            PhotoUtil.showPhoto(this, PictureMimeType.ofImage(), PictureConfig.MULTIPLE, true, false, seleteImgList, this);
        } else {
            PhotoUtil.openPhoto(this, seleteImgList, position);
        }
    }

    /**
     * 图片选取返回
     *
     * @param result
     */
    @Override
    public void onResult(List<LocalMedia> result) {
        seleteImgList.clear();
        seleteImgList.addAll(result);
        imgList.clear();
        for (LocalMedia media : result) {
            imgList.add(PhotoUtil.getPath(media));
        }
        if (imgList.size() < 9) {
            imgList.add(imgList.size(), "");
        }
        imgAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancel() {

    }

    /**
     * 子控件点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_delete:
                deleteImg(position);
                break;
        }
    }

    /**
     * 删除图片
     *
     * @param pos
     */
    private void deleteImg(int pos) {
        seleteImgList.remove(pos);
        imgList.remove(pos);
        if (EmptyUtils.isNotEmpty(imgList.get(imgList.size() - 1))) {
            imgList.add(imgList.size(), "");
        }
        imgAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
